import { RecycleApi } from '@/api/infra/file'
const useRecycleOperate = (initInfo, getRecycleSource) => {
  const { success } = useMessage()
  // 表格操作栏的还原点击事件
  const returnSourceOperateClick = row => {
    initInfo.value.recycleInfo.returnDelete.ids = row.id + ''
    initInfo.value.recycleInfo.returnDelete.type = 1
    returnSource()
  }

  // 表格操作栏的彻底删除点击事件
  const realDeleteSourceOperateClick = row => {
    initInfo.value.recycleInfo.returnDelete.ids = row.id + ''
    initInfo.value.recycleInfo.returnDelete.type = 0
    realDeleteSource()
  }

  // 全选按钮后的菜单还原点击事件
  const returnSourceMenuClick = () => {
    initInfo.value.recycleInfo.returnDelete.type = 1
    const activeIdArr: any = []
    initInfo.value.sourceList.forEach((item: any) => {
      if (item.active) {
        activeIdArr.push(item.id)
      }
    })
    initInfo.value.recycleInfo.returnDelete.ids = activeIdArr.join(',')
    returnSource()
  }

  // 全选按钮后的菜单彻底删除点击事件
  const realDeleteSourceMenuClick = () => {
    initInfo.value.recycleInfo.returnDelete.type = 0
    const activeIdArr: any = []
    initInfo.value.sourceList.forEach((item: any) => {
      if (item.active) {
        activeIdArr.push(item.id)
      }
    })
    initInfo.value.recycleInfo.returnDelete.ids = activeIdArr.join(',')
    realDeleteSource()
  }

  // 还原数据
  const returnSource = () => {
    RecycleApi.return(initInfo.value.recycleInfo.returnDelete).then((res: any) => {
      if (res.code === 200) {
        success('文件已还原到根目录！')
        getRecycleSource()
      }
    })
  }

  // 彻底删除数据
  const realDeleteSource = () => {
    RecycleApi.delete(initInfo.value.recycleInfo.returnDelete).then((res: any) => {
      if (res.code === 200) {
        success('删除成功！')
        getRecycleSource()
      }
    })
  }

  return {
    returnSourceOperateClick,
    realDeleteSourceOperateClick,
    returnSourceMenuClick,
    realDeleteSourceMenuClick
  }
}
export default useRecycleOperate
